package com.example.knowman.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.knowman.ObService.OBKnowCategoryService;
import com.example.knowman.ObService.OBKnowMainService;
import com.example.knowman.ReturnMessage.Message;
import com.example.knowman.ReturnMessage.MessageBackKnowShareSimple;
import com.example.knowman.ReturnMessage.MessageBackLog4;
import com.example.knowman.ReturnMessage.MessageExKnowShareKey;
import com.example.knowman.dao.knowShareSimple;
import com.example.knowman.dao.log4Ex;
import com.example.knowman.entity.knowCategory;
import com.example.knowman.entity.knowShare;
import com.example.knowman.entity.log4operate;
import com.example.knowman.service.IKnowShareService;
import com.example.knowman.service.ILog4operateService;
import com.example.knowman.service.IStaffListService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/knowShare")
public class KnowShareController {
    private final ILog4operateService iLog4operateService;
//
    private final IStaffListService iStaffListService;

    private final IKnowShareService iKnowShareService;

    private final OBKnowCategoryService obKnowCategoryService;

    private final OBKnowMainService obKnowMainService;




    public KnowShareController(IKnowShareService iKnowShareService,ILog4operateService iLog4operateService, IStaffListService iStaffListService
            ,OBKnowCategoryService obKnowCategoryService,OBKnowMainService obKnowMainService) {

        this.iKnowShareService = iKnowShareService;
        this.iLog4operateService = iLog4operateService;
        this.iStaffListService = iStaffListService;
        this.obKnowCategoryService = obKnowCategoryService;
        this.obKnowMainService = obKnowMainService;
    }

    @GetMapping("Insert")
    public ResponseEntity<MessageExKnowShareKey> Insert(@RequestParam Integer knowId,
                                                        @RequestParam String myStaffCode,
                                                        @RequestParam(required = false) String comment) {


        MessageExKnowShareKey res = iKnowShareService.Insert(knowId,myStaffCode,comment);

        //log4
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request1 = attributes.getRequest();
        String ipAddress=request1.getHeader("ip");
        String operator=request1.getHeader("staffCode");
        String content = operator+"新增了一个知识分享表信息，知识id为："+knowId;
        iLog4operateService.insertSingle(operator,content,ipAddress);
        //
        if (res.getCode() == 200) {
            return ResponseEntity.ok(res);
        } else {
            return ResponseEntity.badRequest().body(res);
        }
    }


    //更新，能改变 评价（但是只点评1次，不一定可以改）、状态
    @GetMapping("Update")
    public ResponseEntity<Message> update(@RequestParam Integer recId,
                                          @RequestParam(required = false) Integer readState,
                                          @RequestParam(required = false) String comment) {
        Message res = iKnowShareService.updateSimple(recId,readState,comment);

        //log4
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request1 = attributes.getRequest();
        String ipAddress=request1.getHeader("ip");
        String operator=request1.getHeader("staffCode");
        String content = operator+"更新了一个知识分享表信息，知识分享表id为："+recId;
        iLog4operateService.insertSingle(operator,content,ipAddress);
        //
        if (res.getCode() == 200) {
            return ResponseEntity.ok(res);
        } else {
            return ResponseEntity.badRequest().body(res);
        }
    }

    //点赞
    @GetMapping("praise")
    public ResponseEntity<Message> praise(@RequestParam Integer recId) {
        Message mes = new Message();
        //修改knowShare的点赞状态
        iKnowShareService.praise(recId);

        //得到knowId
        knowShare it = iKnowShareService.getByRecId(recId);

        //knowMain对应点赞数+1 (根据knowId)
        obKnowMainService.addPraise(it.getKnowId());


        mes.setCode(200);
        mes.setTip("点赞成功");

        return ResponseEntity.ok(mes);
    }

    //显示knowShare数据（要不要 +知识类别名？ 发布人名字？）
    @GetMapping("getAll")
    public ResponseEntity<MessageBackKnowShareSimple> getAll(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                                   @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                                   @RequestParam(required = false) String knowTitle,
                                                                   @RequestParam(required = false) Integer knowCateId,
                                                                   @RequestParam(required = false) String myStaffCode) {

        //返回ipage
        IPage<knowShare> res = iKnowShareService.getAll(pageNum,pageSize,knowTitle,knowCateId,myStaffCode);

        //返回知识类别Map<knowCateId，知识名>
        Map<Integer,String> KCL = obKnowCategoryService.getIdAndName();

        //赋值

        IPage<knowShareSimple> records = new Page<>(pageNum, pageSize);
        List<knowShareSimple> recordsList = new ArrayList<>();
        for (knowShare item:res.getRecords()){
            knowShareSimple temp = new knowShareSimple();

            temp.setRecId(item.getRecId());
            temp.setKnowCateId(item.getKnowCateId());
            temp.setKnowCateName(KCL.get(item.getKnowCateId()));
            temp.setKnowTitle(item.getKnowTitle());
            temp.setStaffCode4Post(item.getStaffCode4Post());
            temp.setArchDeptName(item.getArchDeptName());
            temp.setKnowId(item.getKnowId());

            recordsList.add(temp);
        }

        records.setTotal(res.getTotal());
        records.setRecords(recordsList);

        //写message
        MessageBackKnowShareSimple mes = new MessageBackKnowShareSimple();
        mes.setCode(200);
        mes.setTip("查询成功");
        mes.setList(records);
        return ResponseEntity.ok(mes);
    }



    //删除
    @GetMapping("DeleteByRecId")
    public ResponseEntity<Message> DeleteByRecIds(@RequestParam Integer recId) {


        Message mes  = iKnowShareService.deleteBykey(recId);
        //log4
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request1 = attributes.getRequest();
        String ipAddress=request1.getHeader("ip");
        String operator=request1.getHeader("staffCode");
        String content = operator+"删除了一个知识分享表信息，知识分享表id为："+recId;
        iLog4operateService.insertSingle(operator,content,ipAddress);
        //
        if (mes.getCode() == 200) {
            return ResponseEntity.ok(mes);
        } else {
            return ResponseEntity.badRequest().body(mes);
        }
    }
}
